<?php
ini_set('display_errors', 1);
error_reporting(-1);
ini_set('memory_limit', '1024M');
require_once '/data/wwwroot/g_vendor/autoload.php';

use Biz\model\brandAccount\BrandAccountAuth as mBrandAccountAuth;
use Biz\model\brandAccount\BrandAccountInfo;
use Biz\service\BrandRemind\BrandLevelRemind as sBrandLevelRemind;

brandExpired29Days();

function brandExpired29Days()
{
    $where = [
        'type' => BrandAccountInfo::ACCOUNT_TYPE_BRAND,
        'account_status' => BrandAccountInfo::ACCOUNT_STATUS_EXPIRE,
        'in_end_time' => date('Y-m-d', strtotime('-29 days'))
    ];

    $accountList = BrandAccountInfo::getAllByWhere($where, ['id', 'brand_id', 'uid', 'in_start_time', 'in_end_time']);

    if ($accountList) {

        $accountIds = array_column($accountList, 'id');

        $accountAuths = mBrandAccountAuth::getAllByWhere([
            'account_id' => $accountIds,
            'auth_status' => [mBrandAccountAuth::AUTH_STATUS_USED, mBrandAccountAuth::AUTH_STATUS_EXPIRE],
            'ORDER' => ['id' => 'DESC'],
        ]);

        $accountAuthsMap = [];

        foreach ($accountAuths as $auth) {
            $accountAuthsMap[$auth['account_id']][] = $auth;
        }

        foreach ($accountList as $account) {

            $uid = $account['uid'];

            $phone = '';

            if (isset($accountAuthsMap[$account['id']])) {
                $auths = $accountAuthsMap[$account['id']];
                $lastAuth = empty($auths) ? [] : $auths[0];
                $phone = $lastAuth['phone'] ?? '';
                if (empty($phone) || strlen($phone) != 11 || strpos($phone, '142') === 0) {
                    $phone = '';
                }
            }

            var_dump('过期29天',[
                'brand_id' => $account['brand_id'],
                'uid' => $uid,
                'phone' => $phone,
                'in_start_time' => $account['in_start_time'],
                'in_end_time' => $account['in_end_time'],
            ]);

            $reminder = new sBrandLevelRemind();

            $reminder->handle(sBrandLevelRemind::REMIND_SCENE_BRAND_EXPIRE_29DAYS, $uid, $phone);

        }
    }
}

